#include "Random.h"
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;

typedef unsigned char uchar;

int random()
{
    int r, r1 = 0, r2, r3, memory_r, t;
    static int calls = 1;
    static int last = 0;
    srand(memory_r % 453);
    for (int i = 0; i < rand() % 20; i++)
		r1 += rand() + i;
    memory_r = memory_r % 1000;
    t = time(0);
    srand(memory_r);
    r2 = rand();
    srand(t);
    r3 = rand();
    r = r1 + r2 + r3 + ((t*12)%3) + memory_r - (calls * 3)%2 + 8;
    calls++;
    if (calls % 2)
		r = 632453 - r;
	else
		r = 345 + r * rand();
	if (last)
		r *= last;
	last = abs(r);
    return last;
}

bool random_p(uchar p)
{
	if (p == 100)
		return true;
	p--;
	if (p >= 100)
		return false;
	return (((random() + 1) % 100) <= p);
}

int per_procent(int total, int pr)
{
    return total * pr / 100;
}

int random_range(int from, int to)
{
	if (from > to)
	{
        int t = to;
        to = from;
        from = t;
	}
    int total = to - from;
    int rand = random() % total;
    return from + rand;
}

size_t random_range(size_t from, size_t to)
{
	if (from > to)
	{
        size_t t = to;
        to = from;
        from = t;
	}
    size_t total = to - from;
    size_t rand = random() % total;
    return from + rand;
}
